import static java.lang.StrictMath.PI;
import static java.lang.StrictMath.abs;
import java.util.Random;
import model.ActionType;
import model.Game;
import model.Hockeyist;
import model.HockeyistState;
import model.Move;
import model.Unit;
import model.World;

/**
 *
 * @author Constantine Tretyak
 */
public class DefaultAttack extends AbstractAttack {

    public DefaultAttack(Hockeyist self, World world, Game game, Move move, long puckOwner, long first) {
        super(self, world, game, move, puckOwner, first);
    }

    @Override
    protected void firstTeammate() {
        if (AGGRESSIVE_ATTACK_OPP_TICKS > 0) {

            Unit bitOpp = null;
            for (Unit opp : getOpponents(getUnitById(puckOwner))) {
                if (((Hockeyist) opp).getState() != HockeyistState.KNOCKED_DOWN) {
                    bitOpp = opp;
                    
                    break;
                }
            }

            move.setTurn(self.getAngleTo(bitOpp));
            move.setSpeedUp(1.0d);

            
            if (self.getDistanceTo(bitOpp) < game.getStickLength() && moving.atSector(self, bitOpp, game.getStickSector())) {
                move.setAction(ActionType.STRIKE);
            }
            if (oppBlockUnit(self)) {
                move.setAction(ActionType.STRIKE);
                move.setSpeedUp(1.0d);
            }
//
            if (oppFrontMeWithPuck(self) && self.getDistanceTo(puck) > game.getStickLength()) {
                move.setAction(ActionType.STRIKE);
                move.setSpeedUp(1.0d);
            }
            return;
        }
        if (AGGRESSIVE_ATTACK_OPP_TICKS > 0) {
            for (Unit opp : getOpponents(getUnitById(puckOwner))) {
                if (((Hockeyist) opp).getState() != HockeyistState.KNOCKED_DOWN) {
                    moving.moveTo(opp, opp);
                    break;
                }
            }
        }
        defendNet(false);
    }

    @Override
    protected void puckOwner() {
        // если шайба у меня

        if (puckY == 0) {   // если шайба разводится
            moving.moveTo(new Point(30, coords.getX(-0.5d), coords.getYcoord(new Random().nextDouble() - 0.5d)), null);
            return;
        }

        // Лучшая точка атаки
        double yAttack;
        double xAttack;
        if (selfX > 0.2d) {
            yAttack = coords.getY(game.getGoalNetTop());
            if (Math.signum(yAttack) != Math.signum(selfY)) {
                yAttack = 0 - yAttack;
            }
            yAttack = yAttack * 2;
            xAttack = -0.7920792079207921d;
        } else {
//            yAttack = coords.getY(getNet(false).getY());
//            xAttack = coords.getX(getNet(false).getX());
//            xAttack -= 0.1d * Math.signum(xAttack);
            yAttack = coords.getY(game.getGoalNetTop());
            if (Math.signum(yAttack) != Math.signum(selfY)) {
                yAttack = 0 - yAttack;
            }
            xAttack = -0.7920792079207921d;
        }
//        moving.moveTo(new Point(50, coords.getXcoord(xAttack), coords.getYcoord(yAttack)), null);
        moving.moveExactly(new Point(5, coords.getXcoord(xAttack), coords.getYcoord(yAttack)), getNet(false));
        if (abs(self.getAngleTo(new Point(50, coords.getXcoord(xAttack), coords.getYcoord(yAttack)))) < 30 * PI / 180) {
            move.setSpeedUp(1.0d);
        }
    }
}
